package com.gutai.admin.common.util;

import java.util.List;
import java.util.Map;

import com.gutai.admin.common.entity.response.ListResponse;
import com.gutai.admin.common.entity.response.MapResponse;
import com.gutai.admin.common.entity.response.MsgResponse;
import com.gutai.admin.common.entity.response.ObjectResponse;
import com.gutai.admin.common.entity.response.PageResponse;
import com.gutai.admin.common.entity.response.Response;

/**
 * 
 * 描述：
 * 
 * <pre>
 * HISTORY
 * ****************************************************************************
 *  ID    DATE            PERSON            REASON
 *  1     2018年12月2日         YC           Create
 * ****************************************************************************
 * </pre>
 * 
 * @author YC
 * @version 1.0
 */
public class ResponseHelper {

    public static final String OK = "ok";

    public static final String FAIL = "fail";

//    public static <T> Response<T> buildOk(List<T> obj) {
//        return new Response("ok", obj);
//    }
//
//    public static <T> Response<T> buildOk(T obj) {
//        return new Response("ok", Arrays.asList(obj));
//    }
   
    private ResponseHelper() {
    }

    public static Response buildOk() {
        return new MsgResponse(OK);
    }

    @SuppressWarnings("rawtypes")
    public static Response buildOkWithPage(List result, long totalSize) {
        PageResponse resp = new PageResponse();
        resp.setTotalSize(totalSize);
        resp.setResult(result);
        resp.setSucc(OK);
        return resp;
    }

    // public static Response buildOk(String msg) {
    // return build(OK, null, msg);
    // }

    public static Response buildOk(Object result) {
        return build(OK, result, null, null);
    }

    public static Response buildOk(List result, Long totalNum) {
        return build(OK, result, null, Long.toString(totalNum));
    }

    private static Response build(String succ, Object result, String msg, Object resParam) {
        Response resp;

        if (result instanceof List) {
            if (resParam != null) {
                resp = new ListResponse((List) result, Integer.valueOf((String) resParam));
            } else {
                resp = new ListResponse((List) result);
            }
        } else if (result instanceof Map) {
            resp = new MapResponse();
        } else if (result instanceof String) {
            resp = new MsgResponse();
        } else {
            resp = new ObjectResponse();
        }
        resp.setResult(result);
        resp.setSucc(succ);
        resp.setMsg(msg);
        return resp;
    }

    public static Response buildFail(String msg) {
        return build(FAIL, null, msg, null);
    }

    public static Response buildFail(Object result, String msg) {
        return build(FAIL, result, msg, null);
    }
}
